package emailBox

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/emailBox"
	emailBoxReq "github.com/flipped-aurora/gin-vue-admin/server/model/emailBox/request"
)

type EmailConfigService struct {
}

// CreateEmailConfig 创建EmailConfig记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailConfigService *EmailConfigService) CreateEmailConfig(emailConfig emailBox.EmailConfig) (err error) {
	err = global.GVA_DB.Create(&emailConfig).Error
	return err
}

// DeleteEmailConfig 删除EmailConfig记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailConfigService *EmailConfigService) DeleteEmailConfig(emailConfig emailBox.EmailConfig) (err error) {
	err = global.GVA_DB.Delete(&emailConfig).Error
	return err
}

// DeleteEmailConfigByIds 批量删除EmailConfig记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailConfigService *EmailConfigService) DeleteEmailConfigByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]emailBox.EmailConfig{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateEmailConfig 更新EmailConfig记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailConfigService *EmailConfigService) UpdateEmailConfig(emailConfig emailBox.EmailConfig) (err error) {
	err = global.GVA_DB.Save(&emailConfig).Error
	return err
}

// GetEmailConfig 根据id获取EmailConfig记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailConfigService *EmailConfigService) GetEmailConfig(id string) (emailConfig emailBox.EmailConfig, err error) {
	err = global.GVA_DB.Where("uuid = ?", id).First(&emailConfig).Error
	return
}

// GetEmailConfigInfoList 分页获取EmailConfig记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailConfigService *EmailConfigService) GetEmailConfigInfoList(info emailBoxReq.EmailConfigSearch) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&emailBox.EmailConfig{})
	var emailConfigs []emailBox.EmailConfig
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Find(&emailConfigs).Error
	return emailConfigs, total, err
}
